//
//  HTMyAnnotationView.m
//  TruckUser
//
//  Created by Ting Han on 2017/10/17.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTMyAnnotationView.h"
#import "HTMyAnnotation.h"
#import "HTDriverMainModel.h"

#define kCalloutWidth   210.0
#define kCalloutHeight  188.0

#define kWidthSelf  150.f
#define kHeightSelf 60.f

@interface HTMyAnnotationView()

@property (nonatomic,strong) HTDriverMainModel *driverMainModel;


@end

@implementation HTMyAnnotationView

#pragma mark - Override

- (void)setSelected:(BOOL)selected
{
    [self setSelected:selected animated:NO];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    if (self.selected == selected)
    {
        return;
    }
    
    if (selected)
    {
        if (self.calloutView == nil)
        {
            [self netLoadData];
           
        }
       
    }
    else
    {
        [self.calloutView removeFromSuperview];
        self.calloutView = nil;
    }
    [super setSelected:selected animated:animated];
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    BOOL inside = [super pointInside:point withEvent:event];
    if (!inside && self.selected)
    {
        inside = [self.calloutView pointInside:[self convertPoint:point toView:self.calloutView] withEvent:event];
    }
    return inside;
}

#pragma mark - Life Cycle

- (id)initWithAnnotation:(id<MAAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    
    if (self)
    {
        self.bounds = CGRectMake(0.f, 0.f, kWidthSelf, kHeightSelf);
        self.backgroundColor = [UIColor clearColor];
        
    }
    return self;
}

#pragma mark --- 网络请求
- (void)netLoadData{
    
    if (self.ID == nil || self.ID.length <= 0) {
        return;
    }
    YDHUD *hud = [YDHUD juhuaHUD];
    [[UIApplication sharedApplication].keyWindow addSubview:hud];
    [hud showAnimated:YES];
    [HTHttpRequestManager requestTruckDriverCarInfoWithDriverid:self.ID success:^(id  _Nullable responseObject) {
        [hud hideAnimated:YES];
        DBLog(@"司机信息：%@",responseObject);
        if (!responseObject) {
            [HTShowUtil showMessage:@"司机信息请求失败"];
            return;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
            [HTShowUtil showMessage:responseObject[@"message"]];
            return;
        }
        self.driverMainModel = [HTDriverMainModel mj_objectWithKeyValues:responseObject[@"data"]];
        self.calloutView = [[HTCalloutView alloc] initWithFrame:CGRectMake(0, 0, kCalloutWidth, kCalloutHeight)];
        self.calloutView.center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f + self.calloutOffset.x,-CGRectGetHeight(self.calloutView.bounds) / 2.f + self.calloutOffset.y);
        if ([self.annotation isKindOfClass:[HTMyAnnotation class]]) {
            self.calloutView.ID = self.ID;
            self.calloutView.driverMainModel = self.driverMainModel;
        }
        [self addSubview:self.calloutView];
    } failure:^(NSError * _Nonnull error) {
        [hud hideAnimated:YES];
        [HTShowUtil showMessage:@"司机信息请求失败"];
    }];
}

@end
